Conversation
CheezItMan
left a comment
There was a problem hiding this comment.
You did well and they all work, but you made some mistakes on the big-O due to the fact that you misunderstood what was happening with some of the built-in functions.
You are also doing a very odd search where you split the array, but you aren't doing a binary search, you search both halves. Take a look at my comment on that one.
lib/recursive-methods.rb
Outdated
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) where n/2 recursive calls are made |
There was a problem hiding this comment.
Not quite, as s[1..s.length-1] makes a new string with n-2 characters, and since you do this every recursive call... you end up with O(n * n) = O(n^2).
There is another way by using a helper method and keeping track of the current indexes which is O(n).
There was a problem hiding this comment.
Gotcha. For some reason I thought accessing a slice of an array/string would access part of what was already stored in memory.
lib/recursive-methods.rb
Outdated
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) where n/2 recursive calls are made | ||
| # Space complexity: O(n) where the max depth of the function is n/2 |
There was a problem hiding this comment.
Similarly each recursive call makes a new array so O(n^2).
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: If n < m, time complexity will be O(log10 n) since the number of calls is dependent on how many times the smaller number can be divided by 10 |
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) where n recursive calls are made |
lib/recursive-methods.rb
Outdated
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) where n recursive calls are made |
There was a problem hiding this comment.
Not quite since s[0..-1] makes a new string, it's O(n^2)
It's similar for Space complexity.
lib/recursive-methods.rb
Outdated
| if s.length == 1 || s.length == 0 | ||
| return s | ||
| else | ||
| s[-1] + reverse_inplace(s[1...-1]) + s[0] |
There was a problem hiding this comment.
Unfortunately this isn't in place. It makes a new string.
| if n <= 0 | ||
| return 0 | ||
| else | ||
| return 2 + bunny(n - 1) |
lib/recursive-methods.rb
Outdated
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) where n/2 recursive calls are made |
| if array.length == 1 | ||
| return true if array[0] == value | ||
| else | ||
| first_half = array[0...(array.length / 2)] |
There was a problem hiding this comment.
This looks like an attempt at MergeSort or something. Because you're making new arrays, this will be O(n^2), but I don't see what advantage you're getting by splitting it.
This could be easier as:
return false if array.length == 0
return true if value == array[0]
return search(array[1...-1])There was a problem hiding this comment.
Wouldn't this save stack memory (space complexity) though? At most, the method will only go log2(n) levels deep at a time, as opposed to n levels if you just knocked off one number with each call.
There was a problem hiding this comment.
It will save on some stack space, but won't save you in time complexity.
CheezItMan
left a comment
There was a problem hiding this comment.
Much better reverse_in_place method.
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n)^2 where n/2 recursive calls are made and each calls slice, which is O(n) |
There was a problem hiding this comment.
now that you're doing this with swap, this method is O(n), well done!
lib/recursive-methods.rb
Outdated
| if s.length == 1 || s.length == 0 | ||
| return s | ||
| else | ||
| s[-1] + reverse_inplace(s[1...-1]) + s[0] |
No description provided.